<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Controllers
 * @see 		Articles
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Articles extends CI_Controller {
	var $cat = '';
	var $q = '';
	var $limit = '';
	var $orderby = '';
	var $ordermode = '';
	var $updated = '';
	var $failed = '';
	var $name = '';
	var $error = '';
	var $uri_arr = array();
	var $uri_str = '';
	var $site_url = '';

	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (check_rights() == false)
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'Art', true);
		$this->q = $this->input->get('q', true);
		$this->cat = $GLOBALS['var']['filter_cat'];
		$this->orderby = $this->input->get('orderby', true);
		$this->ordermode = $this->input->get('ordermode', true);
		$this->updated = $this->input->get('updated', true);
		$this->failed = $this->input->get('failed', true);
		$this->name = $this->input->get('name', true);
		$this->error = $this->input->get('error', true);
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'], 
			'cat' => $this->cat,
			'q' => $this->q,
			'rowstart' => $GLOBALS['var']['rowstart'],
			'limit' => $this->limit,
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode
		);
		$this->uri_str = url_uri($this->uri_arr);
		$this->site_url = site_url($GLOBALS['var']['act']);
		$this->image_w = $GLOBALS['cfg']['news_image_width'];
		$this->image_h = $GLOBALS['cfg']['news_image_height'];
		$this->thumb_w = $GLOBALS['cfg']['news_thumb_width'];
		$this->thumb_h = $GLOBALS['cfg']['news_thumb_height'];
		$this->gallery_w = $GLOBALS['cfg']['news_gallery_width'];
		$this->gallery_h = $GLOBALS['cfg']['news_gallery_height'];
	}

	public function index()
	{
		/*
		* Xu ly du lieu
		*/
		$footer_cols = array(
			'1' => 'Cột 1',
			'2' => 'Cột 2',
			'3' => 'Cột 3',
			'4' => 'Cột 4'
    	);
		$num_rows = $this->Art->show($this->uri_arr, true);
		$data = array(
			'orderby' => $this->orderby,
			'ordermode' => $this->ordermode,
			'q' => $this->q,
			'updated' => $this->updated,
			'failed' => $this->failed,
			'error' => $this->error,
			'name' => $this->name,
			'uri_str' => $this->uri_str,
			'site_url' => $this->site_url,
			'articles' => array(),
			'footer_cols' => $footer_cols
		);
		$rows = $this->Art->show($this->uri_arr);
		if($rows) $data['articles'] = $rows;
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Danh sách bài viết',
			'add_link' => current_url().'/update',
			'search' => true,
			'page_list' => page_list($num_rows, $this->uri_arr),
			'datetime_picker' => false,
			'submit_btn' => false,
			'cat_list' => $this->Art->filter_category_list(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/index', $data);
		$this->load->view('footer');
	}

	public function update($id = '')
	{
		/*
		* Load categories model
		*/
		$this->load->model('article_categories_model', 'Cat');
		/*
		* Kiem tra quyen cap nhap
		*/
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Xu ly du lieu
		*/
		$copyid = $this->input->get('copyid', true);
		if ($copyid) $id = $copyid;
		$info = $this->Art->info($id);
		if (!$id)
		{
			$info['date_added'] = date(TIME_SQL);
		}
		if ($copyid)
		{
			$info['id'] = '';
			$info['img'] = '';
		}
		$data = array(
			'updated' => $this->updated,
			'failed' => $this->failed,
			'error' => $this->error,
			'name' => $this->name,
			'info' => $info,
			'action' => site_url($GLOBALS['var']['act'].'/process').$this->uri_str,
			'category_list' => $this->Cat->show(),
			'thumb_width' => $this->thumb_w,
			'thumb_height' => $this->thumb_h,
			'gallery_width' => $this->gallery_w,
			'gallery_height' => $this->gallery_h
		);
		/*
		* Hien thi
		*/
		$header = array(
			'title' => ($id ? 'Cập nhập' : 'Thêm mới').' bài viết',
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => true,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/update', $data);
		$this->load->view('footer');
	}

	public function process()
	{
		/*
		* Kiem tra POST method
		*/
		if(!$_POST) {
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$name_vn = $this->input->post('name_vn', true);
		if(!token_validation() || !$name_vn) {
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Them tham so url
		*/
		$id = $this->input->post('id', true);
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		$this->uri_arr['id'] = $id;
		$this->uri_arr['name'] = str_replace('&', '', $name_vn);
		/*
		* Xu ly du lieu
		*/
		$data = array(
			'name_vn' => $name_vn,
			'name_en' => $this->input->post('name_en', true),
			'keyword' => url_title(viet_decode($name_vn), '-', true),
            'tags' => $this->input->post('tags', true),
            'seo_desc' => $this->input->post('seo_desc', true),
            'seo_title' => $this->input->post('seo_title', true),
            'cat' => $this->input->post('cat', true),
            'active' => $this->input->post('active', true),
            'des_vn' => $this->input->post('des_vn'),
			'des_en' => $this->input->post('des_en'),
			'info_vn' => $this->input->post('info_vn'),
			'info_en' => $this->input->post('info_en'),
            'sitemap' => $this->input->post('sitemap', true),
            'img_alt' => $this->input->post('img_alt', true),
            'date_added' => $this->input->post('date_added', true),
            'date_modified' => date(TIME_SQL)
		);
		/*
		* Upload file
		*/
		$this->load->library('upload');
		$upload_data = array();
		if ($_FILES['img']['name'])
		{
			$upload_data = upload('img', url_title(viet_decode($name_vn), '-', true), NEWS);
			if ($upload_data['file_name'])
			{
				make_thumb($upload_data['full_path'], $upload_data['file_path'].'thumbs/'.$upload_data['file_name'], $this->thumb_w, $this->thumb_h);
				make_thumb($upload_data['full_path'], $upload_data['file_path'].'gallery/'.$upload_data['file_name'], $this->gallery_w, $this->gallery_h);
				$data['img'] = $upload_data['file_name'];
			}
		}
	  	/*
		* Ghi du lieu
		*/
	  	if($this->Art->process($data, $id)) {
	  		$this->uri_arr['updated'] = 1;
	  	} else {
	  		$this->uri_arr['failed'] = 1;
	  	}
	  	/*
		* Chuyen huong
		*/
	  	if($id > 0) {
	  		redirect($GLOBALS['var']['act'].'/update/'.$id.url_uri($this->uri_arr));
	  	} else {
	  		redirect($GLOBALS['var']['act'].url_uri($this->uri_arr));
	  	}
	}

}

/* End of file articles.php */
/* Location: ./application/controllers/articles.php */